package cn.crdeng.controller.admin;

import cn.crdeng.Service.TypeService;
import cn.crdeng.po.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class TypeController {
    @Autowired
    private TypeService typeService;

    /**
     * 查询分类
     * @param pageable
     * @param model
     * @return
     */
    @GetMapping("/types")
    public String list(@PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable
            , Model model) {
        model.addAttribute("pageInfo", typeService.listType(pageable));
        return "admin/types";
    }
    //跳转
    @GetMapping("/types/input")
    public String input(Model model) {
        model.addAttribute("type",new Type());
        return "admin/types-input";
    }

    /**
     * 根据ID查询分类
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/types/{id}/input")
    public String editType(@PathVariable Long id,Model model){
        Type type = typeService.getType(id);
        model.addAttribute("type",type);
        return "admin/types-input";
    }


    /**
     * 添加分类
     * @param type
     * @param result
     * @param attributes
     * @return
     */
    @PostMapping("/types")
    public String post(@Valid Type type, BindingResult result, RedirectAttributes attributes) {
        //校验为空直接返回
        if (result.hasErrors()) {
            String defaultMessage = result.getFieldError().getDefaultMessage();
            //提示添加错误
            attributes.addFlashAttribute("message", defaultMessage);
            return "redirect:/admin/types/input";
        }
        //校验是否已经存在
        Type Name = typeService.findByName(type.getName());
        if (Name!=null){
            attributes.addFlashAttribute("message", "分类已经存在！");
            return "redirect:/admin/types/input";
        }
        Type type1 = typeService.saveType(type);
        if (type1 == null) {
            //提示添加错误
            attributes.addFlashAttribute("message", "添加分类失败！");
        } else {
            //提示成功
            attributes.addFlashAttribute("message", "添加分类成功");
        }
        return "redirect:/admin/types";
    }

    /**
     * 修改分类
     * @param type
     * @param result
     * @param id
     * @param attributes
     * @return
     */
    @PostMapping("/types/{id}")
    public String editPost(@Valid Type type, BindingResult result,@PathVariable Long id, RedirectAttributes attributes) {
        //校验为空直接返回
        if (result.hasErrors()) {
            String defaultMessage = result.getFieldError().getDefaultMessage();
            //提示添加错误
            attributes.addFlashAttribute("message", defaultMessage);
            return "redirect:/admin/types/input";
        }
        //校验是否已经存在
        Type Name = typeService.findByName(type.getName());
        if (Name!=null){
            attributes.addFlashAttribute("message", "分类"+type.getName()+"已经存在！");
            return "redirect:/admin/types/input";
        }
        Type type1 = typeService.updateType(id,type);
        if (type1 == null) {
            //提示添加错误
            attributes.addFlashAttribute("message", "修改分类失败！");
        } else {
            //提示成功
            attributes.addFlashAttribute("message", "修改分类成功");
        }
        return "redirect:/admin/types";
    }


    @GetMapping("/types/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        Type type = typeService.getType(id);
        if(type!=null){
            typeService.deleteType(id);
        }
        attributes.addFlashAttribute("message","删除"+type.getName()+"成功");
        return "redirect:/admin/types";
    }
}
